﻿using UnityEngine;
using System.Collections;
using System.Linq;
using FrenzyIsland.UI;
using System.Collections.Generic;
using Engine.Entities.Character;
using Assets.Game.scripts.ui.models;

[RequireComponent(typeof(AddWorkersModel))]
public class AddWorkersController : MonoBehaviour {

    public UITable agentsTable;
    public GameObject agentItemRenderer;

    private AddWorkersModel model;

    // Use this for initialization
    void Start () {

        model = this.GetComponent<AddWorkersModel>();

        if (this.agentsTable != null && this.agentItemRenderer != null)
        {
            FrenzyPeopleController peopleCtrl = FrenzyPeopleController.instance;

            //chiedo tutti gli agenti disoccupati
            var lstDisoccupati = from SteveBrain brain in peopleCtrl.SteveList
                                 where brain.workplace == null
                                 select brain;

            //li inseriisce nella tabella
            foreach (SteveBrain agent in lstDisoccupati)
            {
                SteveModel agentModel = agent.model;
                GameObject itemRendererInstance = NGUITools.AddChild(this.agentsTable.gameObject, this.agentItemRenderer);

                CharacterItemRendererModel itemModel = itemRendererInstance.GetComponent<CharacterItemRendererModel>();
                if (itemModel != null)
                {
                    itemModel.data = agent;
                }
            }
        }
    }


    public void btnQuit_Click()
    {
        GuiManager gui = UIRoot.list[0].GetComponent<GuiManager>();
        gui.CloseWidget(this.gameObject);
    }
}
